<?php
/**
 * Created by PhpStorm.
 * User: DaveTang
 * Date: 2020/3/3
 * Time: 15:56
 * 用户信息缓存组件
 */

namespace App\Controller\Component;
use Cake\Controller\Component;
use Cake\Cache\Cache;

class OrderCacheComponent extends Component
{

    public $_cache_config = 'orders';
    public $_page_num = 30;
    public function initialize(array $config)
    {
        parent::initialize($config);
    }

    /**
     * 删除订单缓存
     */
    public function deleteList($user_id)
    {
        Cache::delete('list_' . $user_id, $this->_cache_config);
        Cache::delete('total_' . $user_id, $this->_cache_config);
        return  true;
    }

    /**
     * 获取用户订单数量
     */
    public function getTotal($user_id)
    {
        $key = 'total_' . $user_id;
        $data = Cache::read($key, $this->_cache_config);
        if(empty($data)){
            $controller = $this->_registry->getController();
            $controller->loadModel('Orders');
            $data = $controller->Orders->find()
                ->select(['id'])
                ->where(['drwee_id' => $user_id])
                ->count();
        }
        Cache::write($key, $data, $this->_cache_config);
        return  $data;
    }

    /**
     * 获取订单缓存
     */
    public function getVal($order_id)
    {
        $key = 'order_' . $order_id;
        $data = Cache::read($key, $this->_cache_config);
        return empty($data) ? $this->createCache($order_id) : $data;
    }

    /**
     * 创建订单缓存
     */
    public function createCache($order_id)
    {
        $key = 'order_' . $order_id;
        $controller = $this->_registry->getController();
        $controller->loadModel('Orders');
        $data = $controller->Orders->find()
            ->where(['id' => $order_id])
            ->first();
        $cache = empty($data) ? null : $data;
        Cache::write($key, $cache, $this->_cache_config);
        return $cache;
    }

    /**
     * 删除订单缓存
     */
    public function deleteCache($order_id)
    {
        $key = 'order_' . $order_id;
        Cache::delete($key, $this->_cache_config);
        return true;
    }

    /**
     * 获取订单列表缓存
     */
    public function getList($user_id, $page, $status)
    {
        $controller = $this->_registry->getController();
        $controller->loadModel('Orders');
        $key = 'list_' . $user_id;
        $where = [];
        $where['user_id'] = $user_id;
        if(!empty($status)){
            $where['status'] = $status;
        }
        $list = Cache::read($key, $this->_cache_config);
        if(empty($list[$page.'_'.$this->_page_num])){
            $orders = $controller->Orders->find()
                ->select(['id'])
                ->where($where)
                ->order(['add_time Desc'])
                ->limit($this->_page_num)
                ->page($page)
                ->toArray();
            $orders = empty($orders) ? [] : array_column($orders, 'id');
            if(empty($status)){
                Cache::write($key, $orders, $this->_cache_config);
            }
        }else{
            $orders = $list[$page.'_'.$this->_page_num];
        }
        return $orders;
    }

}